From: Yehuda Katz Date: Tue, 6 May 2014 02:46:14 +0000 (-0700) Subject: Add some convenience methods X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~1088 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success//%22http:/www.example.com/cgi/success/?a=commitdiff_plain;h=f0c9fcd215ea9c14c500e741668d3af866d281d3;p=cargo.git Add some convenience methods --- diff --git a/libs/hamcrest-rust b/libs/hamcrest-rust index 138de49f2..de700414a 160000 --- a/libs/hamcrest-rust +++ b/libs/hamcrest-rust @@ -1 +1 @@ -Subproject commit 138de49f217604d22e019afd71c529a7d76643f0 +Subproject commit de700414aab1aaa4461618ce7a516cb24a8e6665 diff --git a/src/cargo/core/errors.rs b/src/cargo/core/errors.rs index 25d5fb746..9a1d27408 100644 --- a/src/cargo/core/errors.rs +++ b/src/cargo/core/errors.rs @@ -107,6 +107,14 @@ impl CargoError { CargoInternalError(error) } + pub fn described(description: T) -> CargoError { + CargoInternalError(Described(description.to_str())) + } + + pub fn other() -> CargoError { + CargoInternalError(Other) + } + pub fn cli_error(self) -> CLIError { match self { CargoInternalError(err) => diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 358f6ca35..cd1b49f15 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -2,7 +2,7 @@ extern crate collections; extern crate serialize; extern crate toml; -use core::errors::{CargoResult,CargoError,ToResult,Described,Other}; +use core::errors::{CargoResult,CargoError,ToResult}; use serialize::{Encodable,Encoder}; use std::{io,fmt}; @@ -76,7 +76,7 @@ impl fmt::Show for ConfigValue { pub fn get_config(pwd: Path, key: &str) -> CargoResult { find_in_tree(&pwd, |file| extract_config(file, key)).to_result(|_| - CargoError::internal(Described(format!("Config key not found: {}", key)))) + CargoError::described(format!("Config key not found: {}", key))) } pub fn all_configs(pwd: Path) -> CargoResult> { @@ -100,7 +100,7 @@ fn find_in_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult) -> CargoR loop { let possible = current.join(".cargo").join("config"); if possible.exists() { - let file = try!(io::fs::File::open(&possible).to_result(|_| CargoError::internal(Other))); + let file = try!(io::fs::File::open(&possible).to_result(|_| CargoError::other())); match walk(file) { Ok(res) => return Ok(res), _ => () @@ -110,7 +110,7 @@ fn find_in_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult) -> CargoR if !current.pop() { break; } } - Err(CargoError::internal(Other)) + Err(CargoError::other()) } fn walk_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult<()>) -> CargoResult<()> { @@ -120,14 +120,14 @@ fn walk_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult<()>) -> CargoResult loop { let possible = current.join(".cargo").join("config"); if possible.exists() { - let file = try!(io::fs::File::open(&possible).to_result(|_| CargoError::internal(Other))); + let file = try!(io::fs::File::open(&possible).to_result(|_| CargoError::other())); match walk(file) { Err(_) => err = false, _ => () } } - if err { return Err(CargoError::internal(Other)); } + if err { return Err(CargoError::other()); } if !current.pop() { break; } } @@ -135,25 +135,25 @@ fn walk_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult<()>) -> CargoResult } fn extract_config(file: io::fs::File, key: &str) -> CargoResult { - let path = try!(file.path().as_str().to_result(|_| CargoError::internal(Other))).to_owned(); + let path = try!(file.path().as_str().to_result(|_| CargoError::other())).to_owned(); let mut buf = io::BufferedReader::new(file); - let root = try!(toml::parse_from_buffer(&mut buf).to_result(|_| CargoError::internal(Other))); - let val = try!(root.lookup(key).to_result(|_| CargoError::internal(Other))); + let root = try!(toml::parse_from_buffer(&mut buf).to_result(|_| CargoError::other())); + let val = try!(root.lookup(key).to_result(|_| CargoError::other())); let v = match val { &toml::String(ref val) => String(val.to_owned()), &toml::Array(ref val) => List(val.iter().map(|s: &toml::Value| s.to_str()).collect()), - _ => return Err(CargoError::internal(Other)) + _ => return Err(CargoError::other()) }; Ok(ConfigValue{ value: v, path: vec!(path) }) } fn extract_all_configs(file: io::fs::File, map: &mut collections::HashMap<~str, ConfigValue>) -> CargoResult<()> { - let path = try!(file.path().as_str().to_result(|_| CargoError::internal(Other))).to_owned(); + let path = try!(file.path().as_str().to_result(|_| CargoError::other())).to_owned(); let mut buf = io::BufferedReader::new(file); - let root = try!(toml::parse_from_buffer(&mut buf).to_result(|_| CargoError::internal(Other))); - let table = try!(root.get_table().to_result(|_| CargoError::internal(Other))); + let root = try!(toml::parse_from_buffer(&mut buf).to_result(|_| CargoError::other())); + let table = try!(root.get_table().to_result(|_| CargoError::other())); for (key, value) in table.iter() { match value { @@ -174,11 +174,11 @@ fn extract_all_configs(file: io::fs::File, map: &mut collections::HashMap<~str, fn merge_array(existing: &mut ConfigValue, val: &[toml::Value], path: &str) -> CargoResult<()> { match existing.value { - String(_) => return Err(CargoError::internal(Other)), + String(_) => return Err(CargoError::other()), List(ref mut list) => { let new_list: Vec> = val.iter().map(|s: &toml::Value| toml_string(s)).collect(); if new_list.iter().any(|v| v.is_err()) { - return Err(CargoError::internal(Other)); + return Err(CargoError::other()); } else { let new_list: Vec<~str> = new_list.move_iter().map(|v| v.unwrap()).collect(); list.push_all(new_list.as_slice()); @@ -192,6 +192,6 @@ fn merge_array(existing: &mut ConfigValue, val: &[toml::Value], path: &str) -> C fn toml_string(val: &toml::Value) -> CargoResult<~str> { match val { &toml::String(ref str) => Ok(str.to_owned()), - _ => Err(CargoError::internal(Other)) + _ => Err(CargoError::other()) } } diff --git a/src/cargo/util/process_builder.rs b/src/cargo/util/process_builder.rs index cffd45798..0a0fb3de1 100644 --- a/src/cargo/util/process_builder.rs +++ b/src/cargo/util/process_builder.rs @@ -3,7 +3,7 @@ use std::path::Path; use std::io; use std::io::process::{Process,ProcessConfig,ProcessOutput,InheritFd}; use collections::HashMap; -use core::errors::{ToResult,CargoResult,CargoError,Described}; +use core::errors::{ToResult,CargoResult,CargoError}; #[deriving(Clone,Eq)] pub struct ProcessBuilder { @@ -68,7 +68,7 @@ impl ProcessBuilder { config.cwd = Some(&self.cwd); let os_path = try!(os::getenv("PATH").to_result(|_| - CargoError::internal(Described("Could not find the PATH environment variable".to_owned())))); + CargoError::described("Could not find the PATH environment variable"))); let path = os_path + PATH_SEP + self.path.connect(PATH_SEP); @@ -76,7 +76,7 @@ impl ProcessBuilder { config.env = Some(path.as_slice()); Process::configure(config).map(|mut ok| ok.wait_with_output()).to_result(|_| - CargoError::internal(Described("Could not spawn process".to_owned()))) + CargoError::described("Could not spawn process")) } fn build_config<'a>(&'a self) -> io::IoResult> {